package com.webmining.sherlock.util;


import com.webmining.sherlock.dao.UserDao;
import com.webmining.sherlock.dao.UserDaoForMap;
import org.json.simple.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DataValidator {

    public static final String USER_DOES_NOT_EXIST_ERROR_MESSAGE = "Such user does not exist!";
    public static final String WRONG_PASSWORD = "Wrong password!";

    public static final String USER_ALREADY_EXISTS_ERROR_MESSAGE = "Such user already exists!";
    public static final String TOO_SHORT_PASSWORD_ERROR_MESSAGE = "Too short password!";
    public static final String WRONG_CONFIRM = "Wrong confirm!";

    public static UserDao userDao = UserDaoForMap.getInstance();

    public static boolean isValidLoginData(HttpServletRequest request, HttpServletResponse response) {

        boolean isValidLoginForm = true;

        String email = request.getParameter("email");
        String password = request.getParameter("password");

        if (!userDao.userExists(email)) {
            request.setAttribute("loginErrorAttribute", USER_DOES_NOT_EXIST_ERROR_MESSAGE);
            isValidLoginForm = false;
        } else {
            if (userDao.isValidPassword(email, password)){
                request.getSession().setAttribute("loginWelcomeFieldAttribute", email);
                if (userDao.isAdmin(email))
                    request.getSession().setAttribute("isAdmin", true);
            }
            else {
                request.setAttribute("loginErrorAttribute", WRONG_PASSWORD);
                isValidLoginForm = false;
            }
        }

        return isValidLoginForm;
    }

    private static boolean isValidPasswordLength(String password) {
        return password.length() >= 8;
    }

    private static boolean isPasswordsEqual(String password, String passwordConfirm) {
        return password.equals(passwordConfirm);
    }

    public static boolean isValidRegistrationData(HttpServletRequest request, JSONObject response) {

        String email = request.getParameter("email");
        String password = request.getParameter("password");
        String password_confirm = request.getParameter("confirmPassword");

        if (userDao.userExists(email)) {
            response.put("regErrorAttribute", USER_ALREADY_EXISTS_ERROR_MESSAGE);
            //request.setAttribute("loginFieldAttribute", USER_ALREADY_EXISTS_ERROR_MESSAGE);
            return false;
        } else {
            if (!isValidPasswordLength(password)) {
                response.put("regErrorAttribute", TOO_SHORT_PASSWORD_ERROR_MESSAGE);
                //request.setAttribute("passwordFieldAttribute", TOO_SHORT_PASSWORD_ERROR_MESSAGE);
                return false;
            } else {
                if (!isPasswordsEqual(password, password_confirm)){
                    response.put("regErrorAttribute", WRONG_CONFIRM);
                    //request.setAttribute("confirmPasswordFieldAttribute", WRONG_CONFIRM);
                    return false;
                }
            }
        }

        return true;
    }
}
